package everyday3;

import CommonStructure.ListNode;
import org.junit.jupiter.api.Test;
import tool.s;

import java.util.List;

/**
 * @Author: Siantar
 * @Date: 2023-07-13-12:25
 * @Description: 1.0
 */
public class T0234 {
    public boolean isPalindrome(ListNode head) {
        ListNode right = half(head);
        ListNode head2 = reverse(right.next);
        while (head != null && head2 != null) {
            if (head.val != head2.val) return false;
            head=head.next;
            head2=head2.next;
        }
        return true;
    }

    public ListNode half(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast.next != null && fast.next.next != null) {
            System.out.println("half  " + fast.val);
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public ListNode reverse(ListNode head) {
        if (head==null)return null;
        ListNode pre = null;
        ListNode left = head;
        while (left.next != null) {
            System.out.println("reverse  " + left.val);
            ListNode right = left.next;
            left.next = pre;
            pre = left;
            left = right;
        }
        return left;
    }

    @Test
    public void test() {
        ListNode listNode = s.setListNodeByArray(new int[]{1,1,2,1});
        System.out.println(isPalindrome(listNode));
        ;
    }
}
